package com.anolesoft.amljmes.fortysix.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.amljmes.fortysix.controller.MesBzzQtfxController;
import com.anolesoft.amljmes.fortysix.dto.MesBzzPtdDto;
import com.anolesoft.amljmes.fortysix.dto.MesBzzPtjhDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface MesBzzQtfxServiceMapper {
    // 查询未转场的配套单
    @SelectProvider(type=MesBzzQtfxServiceMapper.createSQL.class, method = "queryMesBzzQtfxSQL")
    List<MesBzzPtdDto> queryMesBzzQtfxSQL(MesBzzQtfxController.DemoVO demoVO);
    public static class createSQL {
        public static String queryMesBzzQtfxSQL(MesBzzQtfxController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid as key,a.*,0 as pageNo,10 as pageSize from mes_bzz_ptd a where a.state!=3 and a.delete_tag=1");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSupportingOrderNo())){
                        String supportingOrderNo = demoVO.getSupportingOrderNo();
                        sql.append(" and upper(a.supporting_order_no) = trim(upper(#{supportingOrderNo, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathernumber())){
                        String fathernumber = demoVO.getFathernumber();
                        sql.append(" and upper(a.fathernumber) = trim(upper(#{fathernumber, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getModel())){
                        String model = demoVO.getModel();
                        sql.append(" and upper(a.model) = trim(upper(#{model, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathername())){
                        String fathername = demoVO.getFathername();
                        sql.append(" and upper(a.fathername) = trim(upper(#{fathername, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getBatch())){
                        String batch = demoVO.getBatch();
                        sql.append(" and upper(a.batch) = trim(upper(#{batch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSorties())){
                        String sorties = demoVO.getSorties();
                        sql.append(" and upper(a.sorties) = trim(upper(#{sorties, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getDispatch())){
                        String dispatch = demoVO.getDispatch();
                        sql.append(" and upper(a.dispatch) = trim(upper(#{dispatch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getProportion())){
                        String proportion = demoVO.getProportion();
                        sql.append(" and upper(a.proportion) = trim(upper(#{proportion, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryCode())){
                        String factoryCode = demoVO.getFactoryCode();
                        sql.append(" and upper(a.factory_code) = upper(#{factoryCode, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    //查询已转场的配套单
    @SelectProvider(type=MesBzzQtfxServiceMapper.createSQL1.class, method = "queryMesBzzQjbjSQL")
    List<MesBzzPtdDto> queryMesBzzQjbjSQL(MesBzzQtfxController.DemoVO demoVO);
    public static class createSQL1 {
        public static String queryMesBzzQjbjSQL(MesBzzQtfxController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid as key,a.*,0 as pageNo,10 as pageSize from mes_bzz_ptd a where a.state=3 and a.delete_tag=1");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSupportingOrderNo())){
                        String supportingOrderNo = demoVO.getSupportingOrderNo();
                        sql.append(" and upper(a.supporting_order_no) = trim(upper(#{supportingOrderNo, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathernumber())){
                        String fathernumber = demoVO.getFathernumber();
                        sql.append(" and upper(a.fathernumber) = trim(upper(#{fathernumber, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getModel())){
                        String model = demoVO.getModel();
                        sql.append(" and upper(a.model) = trim(upper(#{model, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathername())){
                        String fathername = demoVO.getFathername();
                        sql.append(" and upper(a.fathername) = trim(upper(#{fathername, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getBatch())){
                        String batch = demoVO.getBatch();
                        sql.append(" and upper(a.batch) = trim(upper(#{batch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSorties())){
                        String sorties = demoVO.getSorties();
                        sql.append(" and upper(a.sorties) = trim(upper(#{sorties, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getDispatch())){
                        String dispatch = demoVO.getDispatch();
                        sql.append(" and upper(a.dispatch) = trim(upper(#{dispatch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getProportion())){
                        String proportion = demoVO.getProportion();
                        sql.append(" and upper(a.proportion) = trim(upper(#{proportion, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryCode())){
                        String factoryCode = demoVO.getFactoryCode();
                        sql.append(" and upper(a.factory_code) = upper(#{factoryCode, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    // 查询配套分包、已转场、已军检的配套单
    @SelectProvider(type=MesBzzQtfxServiceMapper.createSQL2.class, method = "queryMesBzzPtfbSQL")
    List<MesBzzPtdDto> queryMesBzzPtfbSQL(MesBzzQtfxController.DemoVO demoVO);
    public static class createSQL2 {
        public static String queryMesBzzPtfbSQL(MesBzzQtfxController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid as key,a.*,0 as pageNo,10 as pageSize from mes_bzz_ptd a where a.state!=0 and a.delete_tag=1");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSupportingOrderNo())){
                        String supportingOrderNo = demoVO.getSupportingOrderNo();
                        sql.append(" and upper(a.supporting_order_no) = trim(upper(#{supportingOrderNo, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathernumber())){
                        String fathernumber = demoVO.getFathernumber();
                        sql.append(" and upper(a.fathernumber) = trim(upper(#{fathernumber, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getModel())){
                        String model = demoVO.getModel();
                        sql.append(" and upper(a.model) = trim(upper(#{model, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathername())){
                        String fathername = demoVO.getFathername();
                        sql.append(" and upper(a.fathername) = trim(upper(#{fathername, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getBatch())){
                        String batch = demoVO.getBatch();
                        sql.append(" and upper(a.batch) = trim(upper(#{batch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSorties())){
                        String sorties = demoVO.getSorties();
                        sql.append(" and upper(a.sorties) = trim(upper(#{sorties, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getDispatch())){
                        String dispatch = demoVO.getDispatch();
                        sql.append(" and upper(a.dispatch) = trim(upper(#{dispatch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getProportion())){
                        String proportion = demoVO.getProportion();
                        sql.append(" and upper(a.proportion) = trim(upper(#{proportion, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryCode())){
                        String factoryCode = demoVO.getFactoryCode();
                        sql.append(" and upper(a.factory_code) = upper(#{factoryCode, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    // 将配套计划状态更改为未生成
    @Update("    update mes_bzz_ptjh" +
            "      set generatematching_document = 0" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateMesBzzPtjh(MesBzzPtjhDto inFoDtoParam);

    //删除
    @Update("    update mes_bzz_ptd" +
            "      set delete_tag = 0,delete_person = #{deletePerson, jdbcType=VARCHAR},delete_time = sysdate" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int deleteMesBzzQtfx(MesBzzPtdDto inFoDtoParam);

    //将齐套分析变成配套分包
    @Update("    update mes_bzz_ptd" +
            "      set state = 1" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateMesBzzQtfx(String objuid);
    //将状态变成已军检
    @Update("    update mes_bzz_ptd" +
            "      set state = 2" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateMesBzzPtfb(String objuid);

    //修改为齐套
    @Update("update mes_bzz_ptd set is_it_complete = 1 where objuid = #{objuid, jdbcType=VARCHAR}")
    int updateIsItComplete(String objuid);
    //修改为未齐套
    @Update("update mes_bzz_ptd set is_it_complete = 0 where objuid = #{objuid, jdbcType=VARCHAR}")
    int updateIsItComplete1(String objuid);

    @SelectProvider(type = MesBzzQtfxServiceMapper.createSQL3.class, method = "querydmingxi")
    List<MesBzzPtdDto> querydmingxi(String objuid);
    public static class createSQL3 {
        public static String querydmingxi(@Param("objuid") String objuid) {
            return new SQL() {
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append("a.* from mes_bzz_ptd a where a.objuid in (");
                    sql.append(objuid);
                    sql.append(")");
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }
}
